package com.cqc.paoclient.ui.aritcle.article;

import com.cqc.paoclient.http.RetrofitHelper;
import com.cqc.paoclient.model.ArticleModel;
import com.cqc.paoclient.util.NetErrorUtil;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by ${cqc} on 2017/11/28.
 */

public class SynthArticalPresenter extends SynthArticalContract.Presenter {
    int p = 0;

    @Override
    void getInitArticalList(int codeId) {
        disposable.add(RetrofitHelper.createArticalApi().getArticleList(0, codeId)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<ArticleModel>() {
                    @Override
                    public void accept(ArticleModel articleModel) throws Exception {
                        mView.getInitArticalListSucc(articleModel.getItems());
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        NetErrorUtil.handleThrowable(throwable);
                    }
                }));
    }

    @Override
    void LoadMoreArticalList(int codeId) {
        disposable.add(RetrofitHelper.createArticalApi().getArticleList(++p, codeId)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<ArticleModel>() {
                    @Override
                    public void accept(ArticleModel articleModel) throws Exception {
                        mView.getLoadMoreArticalListSucc(articleModel.getItems());
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        NetErrorUtil.handleThrowable(throwable);
                    }
                }));
    }

    @Override
    void getBanners() {
        disposable.add(RetrofitHelper.createArticalApi().getBanners()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<ArticleModel>() {
                    @Override
                    public void accept(ArticleModel articleModel) throws Exception {
                        mView.onBannersSucce(articleModel.getItems());
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        NetErrorUtil.handleThrowable(throwable);
                    }
                }));
    }
}
